Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update TIME and DATE functions #134

Merged
merged 18 commits into from
Oct 31, 2022

Conversation

MitchellGale
Copy link

Description

Add option to accept datetime like string in both TIME and DATE (eg. accept "1999-01-02 12:12:12" for both TIME and DATE.

Strict check on date for testing for valid dates (eg. Don't accept Feb 30th as a valid date)

opensearchsql> SELECT TIME("1999-02-30 21:07:32");                                                                                                                                                                                                          
TransportError(500, 'SemanticCheckException', {'error': {'type': 'SemanticCheckException', 'reason': 'Invalid Query', 'details': 'time:1999-02-30 21:07:32 in unsupported format, please use HH:mm:ss[.SSSSSSSSS]'}, 'status': 400})


opensearchsql> SELECT DATE("1999-02-19 21:07:32");                                                                                                                                                                                                          
fetched rows / total rows = 1/1
+-------------------------------+
| DATE("1999-02-19 21:07:32")   |
|-------------------------------|
| 1999-02-19                    |
+-------------------------------+
opensearchsql> SELECT DATE("1999-02-19");                                                                                                                                                                                                                   
fetched rows / total rows = 1/1
+----------------------+
| DATE("1999-02-19")   |
|----------------------|
| 1999-02-19           |
+----------------------+
opensearchsql> SELECT TIME("1999-02-19 21:07:32");                                                                                                                                                                                                          
fetched rows / total rows = 1/1
+-------------------------------+
| TIME("1999-02-19 21:07:32")   |
|-------------------------------|
| 21:07:32                      |
+-------------------------------+
opensearchsql> SELECT TIME("21:07:32");                                                                                                                                                                                                                     
fetched rows / total rows = 1/1
+--------------------+
| TIME("21:07:32")   |
|--------------------|
| 21:07:32           |
+--------------------+

Issues Resolved

[List any issues this PR will resolve]

Check List

  • New functionality includes testing.
    • All tests pass, including unit test, integration test and doctest
  • New functionality has been documented.
    • New functionality has javadoc added
    • New functionality has user manual doc added
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

…y to parse time out of datetime or date out of datetime.

Signed-off-by: MitchellGale-BitQuill <[email protected]>
@MitchellGale MitchellGale changed the title Added test cases and support for strict date validation. Added abilit… Update TIME and DATE functions Oct 13, 2022
@codecov
Copy link

codecov bot commented Oct 13, 2022

Codecov Report

❗ No coverage uploaded for pull request base (Integ-updateDateFunction@b30d156). Click here to learn what that means.
The diff coverage is n/a.

@@                     Coverage Diff                     @@
##             Integ-updateDateFunction     #134   +/-   ##
===========================================================
  Coverage                            ?   95.10%           
  Complexity                          ?     3073           
===========================================================
  Files                               ?      303           
  Lines                               ?     8254           
  Branches                            ?      610           
===========================================================
  Hits                                ?     7850           
  Misses                              ?      350           
  Partials                            ?       54           
Flag Coverage Δ
query-workbench 62.76% <0.00%> (?)
sql-engine 97.90% <0.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

Copy link

@Yury-Fridlyand Yury-Fridlyand left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mysql> select DATE(TIME('13:49:00')), TIME(DATE('2020-08-26'));

+------------------------+--------------------------+
| DATE(TIME('13:49:00')) | TIME(DATE('2020-08-26')) |
+------------------------+--------------------------+
| 2022-10-13             | 00:00:00                 |
+------------------------+--------------------------+
1 row in set (0.00 sec)
opensearchsql> select DATE(TIME('13:49:00')), TIME(DATE('2020-08-26'));
{'reason': 'Invalid SQL query', 'details': 'date function expected {[STRING],[DATE],[DATETIME],[TIMESTAMP]}, but get [TIME]', 'type': 'ExpressionEvaluationException'}

@Yury-Fridlyand
Copy link

I think you need to apply/cherry-pick 40ffca2 into your branch to fix that.

@@ -651,7 +652,11 @@ private ExprValue exprConvertTZ(ExprValue startingDateTime, ExprValue fromTz, Ex
*/
private ExprValue exprDate(ExprValue exprValue) {
if (exprValue instanceof ExprStringValue) {
return new ExprDateValue(exprValue.stringValue());
try {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be able to remove the if statement and get the same result. The try...catch will cover both cases.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we still need it because it can be a stringValue or a dateValue.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is there a case where an ExprStringValue doesn't have a stringValue()?

@@ -944,6 +949,7 @@ private ExprValue exprSubDateInterval(ExprValue date, ExprValue expr) {
*/
private ExprValue exprTime(ExprValue exprValue) {
if (exprValue instanceof ExprStringValue) {
//try catch

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

todo?

@@ -240,6 +240,11 @@ public void date() {
assertEquals(DATE, expr.type());
assertEquals(new ExprDateValue("2020-08-17"), eval(expr));
assertEquals("date(DATE '2020-08-17')", expr.toString());

expr = dsl.date(DSL.literal(new ExprDateValue("2020-08-17 12:12:00")));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should test with nano seconds too

@@ -795,6 +800,11 @@ public void time() {
assertEquals(TIME, expr.type());
assertEquals(new ExprTimeValue("01:01:01"), eval(expr));
assertEquals("time(TIME '01:01:01')", expr.toString());

expr = dsl.time(DSL.literal(new ExprTimeValue("2020-01-02 01:01:01")));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should test with nano seconds too (separately)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added case for times < 1.0 seconds. 2016222

Signed-off-by: MitchellGale-BitQuill <[email protected]>
Signed-off-by: MitchellGale-BitQuill <[email protected]>
Signed-off-by: MitchellGale-BitQuill <[email protected]>
Signed-off-by: MitchellGale-BitQuill <[email protected]>

public static final DateTimeFormatter TIME_FORMATTER_VARIABLE_NANOS_OPTIONAL =
new DateTimeFormatterBuilder()
.appendPattern("[uuuu-MM-dd HH:mm:ss][HH:mm:ss]")

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have tests for both patterns?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All patterns should have coverage

@@ -650,8 +652,15 @@ private ExprValue exprConvertTZ(ExprValue startingDateTime, ExprValue fromTz, Ex
* @return ExprValue.
*/
private ExprValue exprDate(ExprValue exprValue) {
if (exprValue.type() == TIME) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use a switch on type() here. For TIME, STRING, DATE, and TIMESTAMP?

if (exprValue instanceof ExprStringValue) {
return new ExprTimeValue(exprValue.stringValue());
try {
return new ExprTimeValue(exprValue.stringValue());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not use a switch on type() here. For TIME, STRING, DATE, and TIMESTAMP?

@Yury-Fridlyand Yury-Fridlyand self-requested a review October 20, 2022 22:13
@Yury-Fridlyand
Copy link

@MitchellGale-BitQuill,
can you add support for TIME(HH:mm)? Current format requires seconds as a mandatory part in input string.

@acarbonetto acarbonetto changed the title Update TIME and DATE functions [BLOCKED] Update TIME and DATE functions0 Oct 25, 2022
@acarbonetto acarbonetto changed the title [BLOCKED] Update TIME and DATE functions0 [BLOCKED] Update TIME and DATE functions Oct 25, 2022
@Yury-Fridlyand Yury-Fridlyand self-requested a review October 26, 2022 00:47
Signed-off-by: MitchellGale-BitQuill <[email protected]>
@MitchellGale
Copy link
Author

@Yury-Fridlyand Added support for HH:mm in 61289bb.

@MitchellGale MitchellGale changed the title [BLOCKED] Update TIME and DATE functions Update TIME and DATE functions Oct 27, 2022
@MitchellGale
Copy link
Author

@[MitchellGale-BitQuill](https://github.com/MitchellGale-BitQuill) MitchellGale-BitQuill changed the title [BLOCKED] Update TIME and DATE functions Update TIME and DATE functions [now](https://github.com/Bit-Quill/opensearch-project-sql/pull/134#event-7685087474)

Removed blocking component. It can be fixed in a later PR.

@Yury-Fridlyand Yury-Fridlyand self-requested a review October 28, 2022 01:09
Signed-off-by: MitchellGale-BitQuill <[email protected]>
@@ -67,7 +69,7 @@ public LocalDateTime datetimeValue() {

@Override
public Instant timestampValue() {
return ZonedDateTime.of(date, timeValue(), ZoneId.systemDefault()).toInstant();
return ZonedDateTime.of(date, timeValue(), ZoneId.of("UTC")).toInstant();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Last minor change - you need to revert this as well ... or ... update test to use UTC too.

Signed-off-by: MitchellGale-BitQuill <[email protected]>
…prDateValue.java for timestampValue to use systemDefault instead of UTC time.

Signed-off-by: MitchellGale-BitQuill <[email protected]>
@MitchellGale MitchellGale merged commit bc1507c into Integ-updateDateFunction Oct 31, 2022
andy-k-improving pushed a commit that referenced this pull request Nov 16, 2024
Signed-off-by: Andriy Redko <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants